﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using common;
using System.Configuration;
using util;

namespace GTVAccountingSystem.view.form
{
    public partial class frmConnecting : Form
    {
        public frmConnecting()
        {
            InitializeComponent();
            setLanguage();
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function setLanguage
         * set language
         * no return
         */ 
        public void setLanguage()
        {
            lblIPServer.Text = Constant.RM.GetString("lblIPServer", Constant.CULTURE) + " : ";
            lblDatabaseName.Text = Constant.RM.GetString("lblDatabaseName", Constant.CULTURE) + " : ";
            lblUserServer.Text = Constant.RM.GetString("lblUserServer", Constant.CULTURE) + " : ";
            lblPasswordServer.Text = Constant.RM.GetString("lblPasswordServer", Constant.CULTURE) + " : ";
            btnConnect.Text = Constant.TAB + Constant.RM.GetString("btnConnect", Constant.CULTURE);
            btnTestConnect.Text = Constant.TAB + Constant.RM.GetString("btnTestConnection", Constant.CULTURE);
            this.Text = Constant.RM.GetString("frmConnecting", Constant.CULTURE);
        }
        /* Author : James
         * Date : 12/4/2012
         * function ConnectDatabase , SetConnectDatabase
         * set connect database
         * return true or false
         */

        //begin function test connect and connect databse

        private Boolean ConnectDatabase(string ip, string dbName, string dbUser, string daPassword)
        {
            try
            {
                MySqlConnection con = new MySqlConnection();
                string MyConString = "SERVER=" + ip + ";" +
                                        "DATABASE=" + dbName + ";" +
                                            "UID=" + dbUser + ";" +
                                                "PASSWORD=" + daPassword + ";charset=utf8";  // if no password just leave blank
                con.ConnectionString = MyConString;
                con.Open();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            
        }
        private Boolean SetConnectDatabase()
        {
            try
            {
                Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                //modified detail file config
                _config.AppSettings.Settings["Server"].Value = txtIPServer.Text;
                _config.AppSettings.Settings["Database"].Value = txtDatabaseName.Text;
                _config.AppSettings.Settings["Uid"].Value = txtUserIP.Text;
                _config.AppSettings.Settings["Password"].Value = txtPassword.Text;

                //save detail
                _config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
        private void btnTestConnect_Click(object sender, EventArgs e)
        {
            //test connection
            if (ConnectDatabase(txtIPServer.Text, txtDatabaseName.Text, txtUserIP.Text, txtPassword.Text))
                //connect database true
                MessageBox.Show("Connect database access", "GTV Accounting System Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            else
                //connect database false
                MessageBox.Show("Connect database fail ! please, test your input !!!", "GTV Accounting System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            //test connect and set detail connect. 
            if (ConnectDatabase(txtIPServer.Text, txtDatabaseName.Text, txtUserIP.Text, txtPassword.Text)&&SetConnectDatabase())
            {
                //both return true, call function connect in data util and set this form visible
                util.DataUtil.connect();
                this.Visible = false;
            }
            else
                //one in two function or both function don't have true
                MessageBox.Show("Programming error!", "GTV Accounting System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void frmConnecting_Load(object sender, EventArgs e)
        {
            //load all properties connect server in form
            this.txtIPServer.Text = ConfigurationManager.AppSettings["Server"].ToString();
            this.txtDatabaseName.Text = ConfigurationManager.AppSettings["Database"].ToString();
            this.txtUserIP.Text = ConfigurationManager.AppSettings["uid"].ToString();
            this.txtPassword.Text = ConfigurationManager.AppSettings["PASSWORD"].ToString();
        }
        //end function test connect and connect databse
    }
}
